package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.AboutUsLeaveWord;
import com.wolves.career.service.AboutUsLeaveWordService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-27
 */
@RestController
@RequestMapping("/aboutus")
@CrossOrigin
public class AboutUsLeaveWordController {

    @Autowired
    AboutUsLeaveWordService leaveWordService;

    /**
     * 用户留言
     * @param message
     * @return
     */
    @PostMapping("/saveMessage")
    public R saveMessage(@RequestBody AboutUsLeaveWord message){
        leaveWordService.save(message);
        return R.ok().data("success",true);
    }

    /**
     * 用户查询自己的留言，根据userId查询用户留言
     * @param userId 用户id
     * @return
     */
    @GetMapping("/getMessage/{userId}")
    public R getMessage(@PathVariable String userId){
        QueryWrapper<AboutUsLeaveWord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<AboutUsLeaveWord> list = leaveWordService.list(queryWrapper);
        return R.ok().data("list",list);
    }

    /**
     * 管理员
     * 分页查询用户留言
     * @param pageNo 当前页码
     * @param limit 每页限制展示数量
     * @param message
     * @return
     */
    @PostMapping("/listMessage/{pageNo}/{limit}")
    public R listMessage(@PathVariable long pageNo,
                         @PathVariable long limit,
                         @RequestBody(required = false) AboutUsLeaveWord message){
        QueryWrapper<AboutUsLeaveWord> queryWrapper =
                leaveWordService.getMessageQuery(message);
        Page<AboutUsLeaveWord> page = new Page<>(pageNo,limit);
        IPage<AboutUsLeaveWord> messagePage = leaveWordService.page(page, queryWrapper);
        return R.ok().data("records",messagePage.getRecords())
                .data("total",messagePage.getTotal());
    }

    /**
     * 为了放置前端能够更改审核信息，需要通过将整个message传回来做修改
     * @param message leaveMeWord实体类(包含修改了的审核意见)
     * @return
     */
    @PostMapping("/auditMessage")
    public R auditMessage(@RequestBody AboutUsLeaveWord message){
        leaveWordService.updateById(message);
        return R.ok().data("success",true);
    }

    /**
     * 根据id获取留言
     * @param messageId
     * @return
     */
    @GetMapping("/get/{messageId}")
    public R getMessageById(@PathVariable String messageId){
        return R.ok().data("message",leaveWordService.getById(messageId));
    }
}

